package org.zeroturnaround.javarebel.integration.ibatis.cbp;

import org.zeroturnaround.bundled.javassist.ClassPool;
import org.zeroturnaround.bundled.javassist.CtClass;
import org.zeroturnaround.bundled.javassist.CtMethod;

import org.zeroturnaround.javarebel.integration.support.JavassistClassBytecodeProcessor;


/**
 * @author need4spd, need4spd@cplanet.co.kr
 * @date 2009. 12. 16.
 *
 */
public class ClassInfoCBP extends JavassistClassBytecodeProcessor {

	@Override
	public void process(ClassPool cp, ClassLoader cl, CtClass ct) throws Exception {

		cp.importPackage("org.zeroturnaround.javarebel.integration.ibatis");
//		cp.importPackage("java.util");
//		cp.importPackage("java.lang.reflect");
//
//		ct.addInterface(cp.get(ClassEventListener.class.getName()));
//
//		CtField f = CtField.make("private static boolean isAddEvent = false;", ct);
//		ct.addField(f);

		//CtConstructor[] constructors = ct.getConstructors();
//		CtConstructor[] constructors = ct.getDeclaredConstructors();
//
//		System.out.println("%%%%%%%%%%%%%" + constructors.length);
//		System.out.println("%%%%%%%%%%%%%" + ct.getDeclaredConstructors().length);
//		ct.addMethod(CtNewMethod.make(
//	            "public static void checkAndAdd() {" +
//	            "  if(!isAddEvent) { " +
//	            "   ReloaderFactory.getInstance().addClassReloadListener($0);" +
//	            "   System.out.println(\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\");" +
//	            "	isAddEvent = true; " +
//	            "  } " +
//	            "}",
//	            ct));

//		for (int i = 0; i < constructors.length; i++) {
//			CtConstructor constructor = constructors[i];
//			//System.out.println("!!!!!!!!!" + constructor);
//			//constructor.insertAfter("checkAndAdd();");
//			//constructor.insertAfter("System.out.println(\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\");");
//		}



//		ct.addMethod(CtNewMethod.make(
//	            "public static void clearBO(Class clazz) {" +
//	            "	CLASS_INFO_MAP.remove(clazz);" +
//	            "}",
//	            ct));
//
//	    CtMethod onClassEventMethod = CtNewMethod.make(
//	            "public void onClassEvent(int eventType, Class clazz) {" +
//		    	" System.out.println(\"clazz----------\" + clazz.getName());" +
//	            "}",
//	            ct);
//	    ct.addMethod(onClassEventMethod);


//		ct.addMethod(CtNewMethod.make(
//				"private static void checkReloadFileAndClear() {" +
//				"ReloaderFactory.getInstance().addClassReloadListener(new ClassEventListener() {" +
//				"public void onClassEvent(int eventType, Class klass) {" +
//				"System.out.println("+
//				"\"Class '\" + klass.getName() + \"' was reloaded!\");" +
//				"}"+
//				"public int priority() {" +
//				"return 0;" +
//				"}" +
//				"});" +
//				"}",
//				ct));
		CtMethod m4 = ct.getMethod("getInstance","(Ljava/lang/Class;)Lcom/ibatis/common/beans/ClassInfo;");
		m4.insertBefore("IbatisBORemover.getInstance().checkAndReloadBO($1, CLASS_INFO_MAP);");

	}
}
